<template>
    <div>
        <!-- 主体展示部分 -->
        <div class="content">

            <div class="carousel-img">
                <div style="text-align:center;margin:50px 0 5px 0;"><h3>品牌特卖</h3></div>
                <el-carousel type="card" height="250px" :autoplay="isAutoplay">
                    <el-carousel-item v-for="item in carouselImgs" :key="item.name">
                        <img :src="item.src" alt="" style="height:inherit;width:100%;border:1px solid #eee;box-sizing: border-box;">
                    </el-carousel-item>
                </el-carousel>
            </div>

            <div class="box" v-for="(item, index) in navList" :key="index">
                <div class="box-title"><h3>{{item.name}}<span>专场</span></h3></div>
                <div class="row" >
                    <div class="col-6 " v-for="(img, index) in item.imgs" :key="index">
                        <div class="col-box">
                            <div class="img-box"><img :src="img.src" alt=""></div>
                            <div class="img-desc">
                                <div><span>{{item.name}}介绍,  包赔不赚</span></div>
                                <div class="discount">
                                    <span>10.0折</span>
                                    <span>好东西我们从来都是原价！</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="content-foot">
                <img src="../../assets/images/index_phone/foot.png" alt="">
            </div>
        </div>

        <!-- 侧栏导航区域 -->
        <transition name="el-fade-in-linear">
            <ul class="navs" v-show="showNavs">
                <li v-for="(item, index) in navList" :key="index" :class="{active: active === index}" @click="scrollTo(index)">
                    {{item.name}}
                </li>
            </ul>
        </transition>
    </div>
</template>

<script>
export default {
    data(){
        return {
            active: 0,
            // 是否自动轮播
            isAutoplay: false,
            showNavs: false,
            navList: [
                {
                    name: '华为',
                    imgs: [
                        {src: require('../../assets/images/index_phone/huawei01.jpg')},
                        {src: require('../../assets/images/index_phone/huawei02.jpg')},
                        {src: require('../../assets/images/index_phone/huawei03.jpg')},
                        {src: require('../../assets/images/index_phone/huawei04.jpg')}
                    ]
                },
                {
                    name: '苹果',
                    imgs: [
                        {src: require('../../assets/images/index_phone/iphone01.jpg')},
                        {src: require('../../assets/images/index_phone/iphone02.jpg')},
                        {src: require('../../assets/images/index_phone/iphone03.jpg')},
                        {src: require('../../assets/images/index_phone/iphone04.jpg')}
                    ]
                },
                {
                    name: '三星',
                    imgs: [
                        {src: require('../../assets/images/index_phone/sanxing01.jpg')},
                        {src: require('../../assets/images/index_phone/sanxing02.jpg')},
                        {src: require('../../assets/images/index_phone/sanxing03.jpg')},
                        {src: require('../../assets/images/index_phone/sanxing04.jpg')}
                    ] 
                },
                {
                    name: '小米',
                    imgs: [
                        {src: require('../../assets/images/index_phone/xiaomi01.jpg')},
                        {src: require('../../assets/images/index_phone/xiaomi02.jpg')},
                        {src: require('../../assets/images/index_phone/xiaomi03.jpg')},
                        {src: require('../../assets/images/index_phone/xiaomi04.jpg')}
                    ]
                },
                {
                    name: 'OPPO',
                    imgs: [
                        {src: require('../../assets/images/index_phone/oppo01.jpg')},
                        {src: require('../../assets/images/index_phone/oppo02.jpg')},
                        {src: require('../../assets/images/index_phone/oppo03.jpg')},
                        {src: require('../../assets/images/index_phone/oppo04.jpg')}
                    ]
                },
                {
                    name: 'VIVO',
                    imgs: [
                        {src: require('../../assets/images/index_phone/vivo01.jpg')},
                        {src: require('../../assets/images/index_phone/vivo02.jpg')},
                        {src: require('../../assets/images/index_phone/vivo03.jpg')},
                        {src: require('../../assets/images/index_phone/vivo04.jpg')}
                    ]
                }
            ],
            // 轮播壁纸
            carouselImgs: [
                {src: require('../../assets/images/index_phone/huawei02.jpg'), name: '华为'},
                {src: require('../../assets/images/index_phone/iphone02.jpg'), name: '苹果'},
                {src: require('../../assets/images/index_phone/sanxing02.jpg'), name: '三星'},
                {src: require('../../assets/images/index_phone/xiaomi02.jpg'), name: '小米'},
                {src: require('../../assets/images/index_phone/oppo02.jpg'), name: 'OPPO'},
                {src: require('../../assets/images/index_phone/vivo02.jpg'), name: 'VIVO'},
            ]
        }
    },
    methods:{
        // 滚动监听器
        onScroll(){
            const navContents = document.querySelectorAll('.content .box')
            const offsetTopArr = []
            navContents.forEach(item => {
                offsetTopArr.push(item.offsetTop)
            })

            const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
            if(scrollTop > 500) {
                this.showNavs = true
            }else{
                this.showNavs = false
            }
            let navIndex = 0
            for(let i = 0; i < offsetTopArr.length; i++){
                if(scrollTop >= offsetTopArr[i] - 100){
                    navIndex = i
                }
            }
            this.active = navIndex
        },
        // 跳转到指定的索引元素
        scrollTo(index){
            // 获取目标 offetTop css 选择器是从 1 开始计数，我们是从 0 开始， 所以要 +1
            const targetOffetTop = document.querySelector(`.content .box:nth-child(${index + 2})`).offsetTop
            let scrollTop = document.documentElement.scrollTop || document.body.scrollTop

            // 定义一次跳 50 像素
            const STEP = 3000
            // const STEP = 500

            // 判断往上滑还是往下滑
            if(scrollTop > targetOffetTop){
                // 往上滑
                smoothUp()
            }else{
                // 往下滑
                smoothDwon()
            }
            // 定义往下滑函数
            function smoothDwon() {
                if(scrollTop < targetOffetTop){
                    // 如果和目标相差距离大于等于 STEP 就跳 STEP
                    // 否则直接跳到目标点，目标是为了防止跳过了。
                    if(targetOffetTop - scrollTop >= STEP){
                        scrollTop += STEP
                    }else{
                        scrollTop = targetOffetTop
                    }
                    document.body.scrollTop = scrollTop
                    document.documentElement.scrollTop = scrollTop

                    // 屏幕在绘制下一帧时会回调传给 requestAnimationFrame 的函数
                    requestAnimationFrame(smoothDwon)
                }
            }
            // 定义往上滑函数
            function smoothUp(){
                if(scrollTop > targetOffetTop){
                    if(scrollTop - targetOffetTop >= STEP){
                        scrollTop -= STEP
                    }else{
                        scrollTop = targetOffetTop
                    }
                    document.body.scrollTop = screenTop
                    document.documentElement.scrollTop = scrollTop

                    requestAnimationFrame(smoothUp)
                }
            }
        }
    },
    mounted(){
        window.addEventListener('scroll', this.onScroll)
    },
    destroyed(){
        window.removeEventListener('scroll', this.onScroll)
    }
}
</script>

<style lang="less" scoped>
.content{
    width: 1200px;
    margin: 20px auto 10px auto;
    .content-foot{
        width: 102%;
        height: 380px;
        position: relative;
        img{
            width: inherit;
            height: inherit;
            position: absolute;
            left: -2%;
        }
    }
}
.content .box{
    width: 100%;
    // height: 600px;
    font-size: 36px;
    font-family: Arial;
    padding: 20px;
    // background-color: #7ec384;
    background-color: rgba(180, 233, 138, 0.1);
    border-radius: 10px;
    margin: 2% 0;
    .box-title{
        margin-bottom: 20px;
        h3{
            padding: 0;
            margin:0;
            width: 15%;
            border-bottom: 3px solid #333;
            font-weight: 700;
            font-size: 35px;
            text-align: center;
            span{
                font-weight: normal;
            }
        }
        border-bottom: 1px solid #333;
    }
}
.content .box:nth-child(2n){
    // background-color: #847ec3;
    background-color: rgba(221, 135, 238, 0.1);

}

.col-box{
    .img-box{
        width: 570px;
        height: 270px;
        overflow: hidden;
        border-radius: 10px;
        img{
            height: inherit;
            width: 100%;
            transition: all 0.5s;
            cursor: pointer;
        }
        img:hover{
            transform: scale(1.1);
            opacity: 0.8;
        }
    }
    .img-desc{
        padding: 20px 10px;
        font-size: 18px;
        font-weight: bold;
        color: #333;
        .discount{
            span:nth-child(1){
                font-size: 18px;
                color: #f10582;
                margin-right: 10px;
                font-weight: 700;
            }
            span:nth-child(2){
                color: #4d4d4d;
                font-size: 16px;
                margin-right: 10px;
                font-family: MicrosoftYaHei;
                font-weight: normal;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }
        }
    }
}

.navs{
    position: fixed;
    left: 40px;
    top: 150px;
    z-index: 9;
    background-color: #fff;
    list-style: none;
    text-align: center;
    padding: 10px 5px;
    width: 80px;
    font-size: 0.85rem;
    color: #888;
    // border: 1px solid #e7ddee;
    box-shadow:0 0 25px 3px rgba(129, 167, 238, 0.2) inset;
    border-radius: 5px;
    li{
        padding: 10px 5px;
        border-radius: 5px;
        cursor: pointer;
    }
    li:hover{
        color: #409eff;
    }
    .active{
        color: #fff;
        background-color: #409eff;
    }
    .active:hover{
        color: #fff;
    }
}

.carousel-img{
    margin: 10px 0 15px 0;
    /deep/.el-carousel__indicators--outside{
        display: none;
    }
    /deep/.el-carousel__arrow{
        outline: none;
        height: 50px;
        width: 30px;
        border-radius: 3px;
        font-size: 14px;
    }
   
    .el-carousel__item:nth-child(2n) {
        background-color: #99a9bf;
    }
    
    .el-carousel__item:nth-child(2n+1) {
        background-color: #d3dce6;
    }
}
</style>